package com.cupdata.sms.core;

import com.cupdata.sms.kafka.KafkaDealMsgTemplate;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class FixedSizeSet {
    private final Cache<String, Boolean> cache;

    public FixedSizeSet(int maxSize) {
        // 默认LRU
        this.cache = Caffeine.newBuilder()
                .maximumSize(maxSize)
                .evictionListener((k,v,c)->{
                    KafkaDealMsgTemplate.syncQueue.add(SyncOperatorType.builder().item(k.toString()).type(1).build());
                })
                .build();
    }

    public void add(String element) {
        cache.put(element, Boolean.TRUE);
    }

    public boolean contains(String element) {
        return cache.asMap().containsKey(element);
    }

    public void remove(String element) {
        cache.invalidate(element);
    }

    class SyncIdempotenceKey implements Runnable{
        private final BlockingQueue<String> queue = new LinkedBlockingQueue<>();

        @Override
        public void run() {
            while (true) {
                try {
                    String item = queue.take();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }
    }
}